package com.zxbg.service.impl.proportalServiceImpl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zxbg.dao.*;
import com.zxbg.domain.*;
import com.zxbg.domain.Process;
import com.zxbg.service.impl.systemtoolsServiceImpl.OrganizationalStructureServiceImpl;
import com.zxbg.service.proportalService.ProcessCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ProcessCenterServiceImpl implements ProcessCenterService {
    @Autowired
    AppDao appDao;
    @Autowired
    ProcessRuleContentDao processRuleContentDao;
    @Autowired
    ApprovalDao approvalDao;
    @Autowired
    EmployeeDao employeeDao;
    @Autowired
    DepartmentDao departmentDao;
    @Autowired
    ProcessDao processDao;
    @Autowired
    FormalApplyDao formalApplyDao;
    @Autowired
    OrganizationalStructureServiceImpl organizationalStructureService;
    @Autowired
    ProcessRuleDao processRuleDao;

    @Override
    public String submitForm(String app_id, Object formData, Integer mode, String employee_id, Integer tenant_id, Integer process_id) {
        if(mode == 1) {
            // 如果是提交
            // 获取app对应流程规则code
            String rule_code = appDao.getAppByAppId(app_id, tenant_id).getProcess_rule_code();
            // 获取流程规则对应流程内容
            List<ProcessRuleContent> rule_content = processRuleContentDao.getProcessRuleContent(rule_code, tenant_id);
            boolean ifHasApprover = false;
            // 存储第一个流程节点以及处理人
            ProcessRuleContent firstNode = new ProcessRuleContent();
            // 第一个节点当前处理人
            List<String> firstNodeApprover = new ArrayList<>();
            // 第一个节点所有处理人
            List<String> firstNodeAllApprover = new ArrayList<>();
            // 存储新加的流程节点id
            List<Integer> new_approval_id = new ArrayList<>();
            // 根据流程内容构建审批列表
            for (int i = 0; i < rule_content.size(); i++) {
                // 一个流程内容可能有多个审批人，产生多个审批
                // 获取节点审批人ID
                ProcessRuleContent processRuleContent = rule_content.get(i);
                Map<String, List<String>> map = getCurrentNodeApprover(processRuleContent, employee_id, tenant_id);
                List<String> approval_person = map.get("allApprover");
                // 为每个审批人创建审批节点
                if(approval_person.size() > 0) {
                    if(firstNodeApprover.size() == 0) {
                        firstNodeAllApprover = approval_person;
                        firstNodeApprover = map.get("current");
                        firstNode = processRuleContent;
                    }
                    for (String approver : approval_person) {
                        Approval approval = new Approval();
                        approval.setProcess_code(rule_code);
                        approval.setProcess_content_id(processRuleContent.getId());
                        approval.setTenant_id(tenant_id);
                        approval.setNode_handler(approver);
                        ifHasApprover = true;
                        approvalDao.addNewApproval(approval);
                        new_approval_id.add(approvalDao.getNewestId());
                    }
                }
            }
            if(!ifHasApprover) {
                return "未有审批人，提交失败";
            }
            // 初始化流程
            Process process = new Process();
            process.setProcess_rule_current_node(firstNode.getId());
            process.setProcess_rule_code(rule_code);
            process.setSubmit_employee_id(employee_id);
            process.setCurrent_task_handler(String.join(",", firstNodeApprover));
            process.setProcess_status("inApproval");
            process.setTenant_id(tenant_id);
            process.setApp_id(app_id);
            process.setAll_handler(String.join(",", firstNodeAllApprover));
            // 传入process，返回带form_data_id的process
            processDao.addNewProcess(saveFormData(app_id, formData, process, employee_id));
            Integer newProcessId = processDao.getNewestProcessId();
            for (Integer id : new_approval_id) {
                approvalDao.setApprovalProcessIdById(id, newProcessId);
            }
            if(process_id != -1) {
                // 如果是从草稿状态提交
                processDao.deleteProcessById(process_id);
            }
        } else {
            if(process_id == -1) {
                // 如果是新建草稿
                Process process = new Process();
                process.setSubmit_employee_id(employee_id);
                process.setTenant_id(tenant_id);
                process.setApp_id(app_id);
                processDao.addDraftProcess(saveFormData(app_id, formData, process, employee_id));
            } else {
                // 如果是保存草稿
                updateFormData(process_id, formData);
                processDao.updateDraftProcess(process_id);
            }
        }
        // 保存表单
        return "";
    }

    public List<String> getLineUpEmployeeId(Employee employee) {
        List<String> res = new ArrayList<>();
        String leader = employee.getLeader_id();
        if(leader != null && !leader.equals("")) {
            res.add(leader);
            res.addAll(getLineUpEmployeeId(employeeDao.getEmployeeByEmployeeId(leader, employee.getTenant_id())));
        }
        return res;
    }

    public Map<String, List<String>> getCurrentNodeApprover(ProcessRuleContent processRuleContent, String employee_id, Integer tenant_id) {
        Map<String, List<String>> res = new HashMap<>();
        List<String> approval_person = new ArrayList<>();
        String select_mode = processRuleContent.getSelect_mode();
        String task_mode = processRuleContent.getTask_mode();
        if (select_mode != null && !select_mode.equals("")) {
            // 如果不是提交节点
            if (select_mode.equals("targetPerson")) {
                // 如果是指定人员
                approval_person.add(processRuleContent.getSetting());
                res.put("current", approval_person);
            } else if (select_mode.equals("lineUp")) {
                // 如果是直线上级
                List<String> allLineUp = getLineUpEmployeeId(employeeDao.getEmployeeByEmployeeId(employee_id, tenant_id));
                Integer start = Integer.parseInt(processRuleContent.getSetting().split(",")[0]) - 1;
                Integer end = Integer.parseInt(processRuleContent.getSetting().split(",")[1]) - 1;
                for (int j = 0; j < allLineUp.size(); j++) {
                    if (j >= start && j <= end) {
                        approval_person.add(allLineUp.get(j));
                    }
                }
                if(task_mode.equals("order")) {
                    List<String> temp = new ArrayList<>();
                    if(approval_person.size() > 0) {
                        temp.add(approval_person.get(0));
                    }
                    res.put("current", temp);
                }
            } else if (select_mode.equals("departmentHead")) {
                // 如果是部门负责人
                Integer dept_id = Integer.parseInt(processRuleContent.getSetting());
                String headerString = departmentDao.getDeptById(dept_id).getDept_head();
                if (headerString != null && !headerString.equals("")) {
                    List<String> headers = Arrays.asList(headerString.split(","));
                    approval_person.addAll(headers);
                    if(task_mode.equals("order")) {
                        res.put("current", Arrays.asList(headers.get(0).split(",")));
                    }
                }
            } else if (select_mode.equals("targetDepart")) {
                // 如果是指定部门
                Integer dept_id = Integer.parseInt(processRuleContent.getSetting());
                String worker_id = departmentDao.getDeptById(dept_id).getWorker_id();
                if (worker_id != null && !worker_id.equals("")) {
                    List<String> workers = Arrays.asList(worker_id.split(","));
                    approval_person.addAll(workers);
                    if(task_mode.equals("order")) {
                        res.put("current", Arrays.asList(workers.get(0).split(",")));
                    }
                }
            }
        }
        if(!res.containsKey("current")) {
            res.put("current", approval_person);
        }
        res.put("allApprover", approval_person);
        return res;
    }

    public Process saveFormData(String app_id, Object formData, Process process, String employee_id) {
        String jsonObject = JSON.toJSONString(formData);
        if(app_id.equals("formal_apply")) {
            FormalApply formalApply = JSONObject.parseObject(jsonObject, FormalApply.class);
            formalApply.setApplicant_id(employee_id);
            formalApply.setApplicant_dept(formalApply.getApplicant_dept());
            formalApplyDao.addNewFormalApply(formalApply);
            process.setForm_data_id(formalApplyDao.getNewestId());
        }
        return process;
    }

    public void updateFormData(Integer process_id, Object formData) {
        Process process = processDao.getProcessById(process_id);
        Integer form_data_id = process.getForm_data_id();
        String app_id = process.getApp_id();
        String jsonObject = JSON.toJSONString(formData);
        if(app_id.equals("formal_apply")) {
            FormalApply formalApply = JSONObject.parseObject(jsonObject, FormalApply.class);
            formalApply.setId(form_data_id);
            formalApplyDao.updateFormalApply(formalApply);
        }
    }

    @Override
    public List<Process> getTargetProcessInfo(String key, String mode, String employee_id, Integer tenant_id, String status, String app_id) {
        List<Process> resList = new ArrayList<>();
        switch (mode) {
            case "drafts": {
                List<Process> processLit = processDao.getDraftProcess(employee_id, tenant_id);
                if(processLit.size() > 0) {
                    for (Process process : processLit) {
                        App app = appDao.getAppByAppId(process.getApp_id(), tenant_id);
                        if(app.getApp_name().contains(key)) {
                            process.setApp_name(app.getApp_name());
                            process.setProcess_rule(processRuleDao.getProcessRuleByCode(process.getProcess_rule_code(), tenant_id));
                            resList.add(process);
                        }
                    }
                }
                break;
            }
            case "my_apply": {
                List<Process> processLit = new ArrayList<>();
                if(status.equals("all") && app_id.equals("all")) {
                    processLit = processDao.getAllMyApply(employee_id, tenant_id);
                } else if(!status.equals("all") && app_id.equals("all")) {
                    processLit = processDao.getTargetTypeMyApply(status, employee_id, tenant_id);
                } else if(status.equals("all") && !app_id.equals("all")) {
                    processLit = processDao.getTargetAppMyApply(app_id, employee_id, tenant_id);
                } else {
                    processLit = processDao.getAppTypeMyApply(status, app_id, employee_id, tenant_id);
                }
                if(processLit.size() > 0) {
                    for (Process process : processLit) {
                        App app = appDao.getAppByAppId(process.getApp_id(), tenant_id);
                        if(app.getApp_name().contains(key)) {
                            resList.add(getAppNameEmployee(app, process, tenant_id));
                        }
                    }
                }
                break;
            }
            case "my_todo": {
                List<Process> processLit = new ArrayList<>();
                if(app_id.equals("all")) {
                    processLit = processDao.getAllMyTodo(employee_id.replace("\"", ""), tenant_id);
                } else {
                    processLit = processDao.getAppTypeMyTodo(app_id, employee_id.replace("\"", ""), tenant_id);
                }
                if(processLit.size() > 0) {
                    for (Process process : processLit) {
                        App app = appDao.getAppByAppId(process.getApp_id(), tenant_id);
                        String submitName = employeeDao.getEmployeeByEmployeeId(process.getSubmit_employee_id(), tenant_id).getEmployee_name();
                        if(app.getApp_name().contains(key) || submitName.contains(key)) {
                            resList.add(getAppNameEmployee(app, process, tenant_id));
                        }
                    }
                }
                break;
            }
            case "have_done": {
                List<Approval> approvalList = approvalDao.getAllMyDone(employee_id.replace("\"", ""), tenant_id);
                List<Process> processList = new ArrayList<>();
                for (Approval approval : approvalList) {
                    Process process = processDao.getProcessById(approval.getProcess_id());
                    process.setOperate(approval.getOperate());
                    process.setOperate_node(approval.getProcess_content_id());
                    processList.add(process);
                }
                if(processList.size() > 0) {
                    for (Process process : processList) {
                        App app = appDao.getAppByAppId(process.getApp_id(), tenant_id);
                        String submitName = employeeDao.getEmployeeByEmployeeId(process.getSubmit_employee_id(), tenant_id).getEmployee_name();
                        if(app.getApp_name().contains(key) || submitName.contains(key)) {
                            if(app_id.equals("all") || process.getApp_id().equals(app_id)) {
                                resList.add(getAppNameEmployee(app, process, tenant_id));
                            }
                        }
                    }
                }
                break;
            }
            case "will_do": {
                List<Process> processLit = new ArrayList<>();
                if(app_id.equals("all")) {
                    processLit = processDao.getAllWillTodo(employee_id.replace("\"", ""), tenant_id);
                } else {
                    processLit = processDao.getAppTypeMyWillDo(app_id, employee_id.replace("\"", ""), tenant_id);
                }
                if(processLit.size() > 0) {
                    for (Process process : processLit) {
                        App app = appDao.getAppByAppId(process.getApp_id(), tenant_id);
                        String submitName = employeeDao.getEmployeeByEmployeeId(process.getSubmit_employee_id(), tenant_id).getEmployee_name();
                        String currentHandler = process.getCurrent_task_handler();
                        List<String> curHandlerList = Arrays.asList(currentHandler.split(","));
                        List<String> curHandlerNameList = new ArrayList<>();
                        for (String string : curHandlerList) {
                            curHandlerNameList.add(employeeDao.getEmployeeByEmployeeId(string, tenant_id).getEmployee_name());
                        }
                        if(app.getApp_name().contains(key) || submitName.contains(key) || curHandlerNameList.contains(key)) {
                            List<Employee> employeeList = new ArrayList<>();
                            for (String id : process.getCurrent_task_handler().split(",")) {
                                Employee employee = employeeDao.getEmployeeByEmployeeId(id, tenant_id);
                                employeeList.add(organizationalStructureService.getAllKindsDept(employee));
                            }
                            process.setCurrent_task_employee(employeeList);
                            resList.add(getAppNameEmployee(app, process, tenant_id));
                        }
                    }
                }
            }
        }
        return resList;
    }

    public Process getAppNameEmployee(App app, Process process, Integer tenant_id) {
        process.setApp_name(app.getApp_name());
        List<Employee> employeeList = new ArrayList<>();
        if(process.getAll_handler() != null) {
            for (String employeeId : process.getAll_handler().split(",")) {
                Employee employee = employeeDao.getEmployeeByEmployeeId(employeeId, tenant_id);
                employeeList.add(organizationalStructureService.getAllKindsDept(employee));
            }
        }
        Employee employee = employeeDao.getEmployeeByEmployeeId(process.getSubmit_employee_id(), tenant_id);
        process.setSubmit_employee(organizationalStructureService.getAllKindsDept(employee));
        process.setAll_employee(employeeList);
        process.setProcess_rule(processRuleDao.getProcessRuleByCode(process.getProcess_rule_code(), tenant_id));
        return process;
    }

    @Override
    public Object getFormData(Integer process_id) {
        Process process = processDao.getProcessById(process_id);
        String app_id = process.getApp_id();
        Integer form_id = process.getForm_data_id();
        Object res = new Object();
        if(app_id.equals("formal_apply")) {
            FormalApply formalApply = formalApplyDao.getFormalApplyById(form_id);
            formalApply.setApplicant(employeeDao.getEmployeeByEmployeeId(formalApply.getApplicant_id(), process.getTenant_id()));
            res = formalApply;
        }
        return res;
    }

    @Override
    public boolean deleteProcess(Integer process_id) {
        Process process = processDao.getProcessById(process_id);
        String app_id = process.getApp_id();
        Integer form_id = process.getForm_data_id();
        if(app_id.equals("formal_apply")) {
            formalApplyDao.deleteProcess(form_id);
            processDao.deleteProcessById(process_id);
        }
        return true;
    }

    @Override
    public Process getOneTargetProcess(Integer process_id, Integer operate_node) {
        Process process = processDao.getProcessById(process_id);
        if(operate_node != 1) {
            List<Approval> approvalList = approvalDao.getTargetApprovalByProcessId(process_id);
            List<String> operator = new ArrayList<>();
            for (Approval approval : approvalList) {
                if(approval.getProcess_content_id() == operate_node) {
                    operator.add(approval.getHandler());
                }
            }
            process.setOperator(operator);
        }
        return process;
    }

    @Override
    public List<String> getNodeButtons(Integer process_id, String mode, Integer tenant_id, Integer operate_node) {
        Process process = processDao.getProcessById(process_id);
        String process_rule_code = process.getProcess_rule_code();
        List<ProcessRuleContent> processRuleContentList = processRuleContentDao.getProcessRuleContent(process_rule_code, tenant_id);
        ProcessRuleContent targetNode = new ProcessRuleContent();
        if(mode.equals("my_todo")) {
            Integer targetNodeId = process.getProcess_rule_current_node();
            for (ProcessRuleContent processRuleContent : processRuleContentList) {
                if(processRuleContent.getId().intValue() == targetNodeId) {
                    targetNode = processRuleContent;
                }
            }
        } else if(mode.equals("my_apply")) {
            targetNode = processRuleContentList.get(0);
        } else if(mode.equals("have_done")) {
            for (ProcessRuleContent processRuleContent : processRuleContentList) {
                if(processRuleContent.getId().intValue() == operate_node) {
                    targetNode = processRuleContent;
                }
            }
        }
        if(targetNode.getButton_id().length() > 0) {
            if(mode.equals("have_done")) {
                List<String> res = new ArrayList<>();
                for (String temp : Arrays.asList(targetNode.getButton_id().split(","))) {
                    if(temp.equals("tell")) {
                        res.add("tell");
                    }
                }
                return res;
            }
            return Arrays.asList(targetNode.getButton_id().split(","));
        } else {
            return new ArrayList<>();
        }
    }

    @Override
    public String setApprovalResult(Integer process_id, String result, String employee_id) {
        // 当前流程
        Process process = processDao.getProcessById(process_id);
        // 当前流程所对应所有的审批节点
        List<Approval> approvalList = approvalDao.getTargetApprovalByProcessId(process_id);
        // 获取当前审批人的未审批节点
        Approval notApprovalNode = new Approval();
        for (Approval approval : approvalList) {
            if((approval.getOperate() == null || approval.getOperate().equals("")) && approval.getNode_handler().equals(employee_id)) {
                notApprovalNode = approval;
                break;
            }
        }
        switch (result) {
            case "Approve":
            case "Jump": {
                if(result.equals("Approve")) {
                    // 同意
                    notApprovalNode.setHandler(employee_id);
                    notApprovalNode.setOperate(result);
                    notApprovalNode.setIf_systemtools(0);
                    approvalDao.setOperate(notApprovalNode);
                } else if(result.equals("Jump")) {
                    // 跳过
                    // 获取当前审批节点
                    String temp_employee_id = process.getCurrent_task_handler().split(",")[0];
                    for (int i = 0; i < approvalList.size(); i++) {
                        Approval approval = approvalList.get(i);
                        if((approval.getOperate() == null || approval.getOperate().equals("")) && approval.getNode_handler().equals(temp_employee_id)) {
                            notApprovalNode = approval;
                            break;
                        }
                    }
                    approvalDao.setNodeOtherOperate(getOperateContent(notApprovalNode, result, employee_id));
                }
                Integer notPassContentId = notApprovalNode.getProcess_content_id();
                // 判断当前总节点是否通过
                List<Approval> notPassNode = approvalDao.getNotPassNode(process_id, notPassContentId);
                if(notPassNode.size() == 0) {
                    enterNextBigNode(process, approvalList, notPassContentId);
                } else {
                    // 判断审批方式
                    ProcessRuleContent processRuleContent = processRuleContentDao.getProcessRuleContentById(notPassNode.get(0).getProcess_content_id());
                    String task_mode = processRuleContent.getTask_mode();
                    if(task_mode.equals("order")) {
                        // 如果是顺签，进入当前总节点的下一个节点
                        processDao.setProcessNextOrderNode(process.getProcess_id(), notPassNode.get(0).getNode_handler());
                    } else {
                        // 不是顺签将其他节点都设为已处理
                        for (Approval approval : notPassNode) {
                            approvalDao.setNodeApproved(approval.getId());
                        }
                        enterNextBigNode(process, approvalList, notPassContentId);
                    }
                }
                return "";
            }
            case "Reject": {
                // 拒绝
                processDao.setProcessReject(process_id);
                approvalDao.setNodeReject(notApprovalNode.getId());
                Integer notPassContentId = notApprovalNode.getProcess_content_id();
                List<Approval> notPassNode = approvalDao.getNotPassNode(process_id, notPassContentId);
                if(notPassNode != null) {
                    for (Approval approval : notPassNode) {
                        approvalDao.setNodeApproved(approval.getId());
                    }
                }
                return "";
            }
            case "Stop": {
                // 终止
                processDao.setProcessStop(process);
                // 获取当前审批节点
                String temp_employee_id = process.getCurrent_task_handler().split(",")[0];
                for (Approval approval : approvalList) {
                    if((approval.getOperate() == null || approval.getOperate().equals("")) && approval.getNode_handler().equals(temp_employee_id)) {
                        notApprovalNode = approval;
                        break;
                    }
                }
                approvalDao.setNodeOtherOperate(getOperateContent(notApprovalNode, "stop", employee_id));
                approvalDao.setLeftNodesApproved(process.getProcess_id());
                return "";
            }
            case "Back": {
                // 获取上一个审批的节点
                Approval lastApproved = new Approval();
                for (Approval approval : approvalList) {
                    if(approval.getHandler() != null && !approval.getHandler().equals("")) {
                        lastApproved = approval;
                    }
                }
                Integer process_content_id = lastApproved.getProcess_content_id();
                if(process_content_id == null) {
                    return "不可返回上一节点";
                }
                if(process_content_id.intValue() == notApprovalNode.getProcess_content_id()) {
                    // 上一个节点和当前审批的节点在同一大节点——顺签
                    approvalDao.setNodeBack(getOperateContent(lastApproved, "Back", employee_id));
                    processDao.setCurrentTaskHandlerByProcessId(lastApproved.getNode_handler(), process_id);
                } else {
                    // 涉及到返回上一个大节点
                    process.setProcess_rule_current_node(process_content_id);
                    // 获取上一个节点的审批方式
                    String task_mode = processRuleContentDao.getProcessRuleContentById(process_content_id).getTask_mode();
                    // 获取大节点的所有审批人
                    String all_handler = "";
                    List<Integer> need_id = new ArrayList<>();
                    for (Approval approval : approvalList) {
                        if(approval.getProcess_content_id().intValue() == process_content_id) {
                            String node_handler = approval.getNode_handler();
                            all_handler = all_handler.equals("") ? node_handler : all_handler + "," + node_handler;
                            if(lastApproved.getId().intValue() != approval.getId()) {
                                need_id.add(approval.getId());
                            }
                        }
                    }
                    process.setAll_handler(all_handler);
                    if(task_mode == null || task_mode.equals("")) {
                        // 如果上一个节点是提交节点
                        return "不可返回上一节点";
                    } else if(task_mode.equals("order")) {
                        // 如果是顺签
                        process.setCurrent_task_handler(lastApproved.getNode_handler());
                    } else {
                        process.setCurrent_task_handler(all_handler);
                        // 设置剩余节点状态
                        for (Integer id : need_id) {
                            approvalDao.otherNodesBack(id);
                        }
                    }
                    approvalDao.setNodeBack(getOperateContent(lastApproved, "Back", employee_id));
                    processDao.setProcessBack(process);
                }
                return "";
            }
            default: return "";
        }
    }

    public Approval getOperateContent(Approval approval, String content, String employee_id) {
        approval.setHandler(employee_id);
        approval.setOperate(content);
        String operate_content = approval.getOperate_content();
        if(operate_content == null || operate_content.equals("")) {
            approval.setOperate_content(employee_id + " & " + content);
        } else {
            approval.setOperate_content(operate_content + "," + employee_id + " & " + content);
        }
        return approval;
    }

    /***
     * @Param approvalList:当前流程所对应所有的审批节点
     * @Param notPassContentId:当前审批人的审批节点
     * @Param process:当前流程
     */
    public void enterNextBigNode(Process process, List<Approval> approvalList, Integer notPassContentId) {
        if(approvalList.get(approvalList.size() - 1).getProcess_content_id() == notPassContentId) {
            // 没有下一个总节点 结束流程
            processDao.setProcessEnd(process.getProcess_id());
        } else {
            // 进入下一个总节点
            Integer nextContentId = -1;
            // 存储下个大节点所需要的审批节点
            List<Approval> nextApprovals = new ArrayList<>();
            for (Approval approval : approvalList) {
                if(approval.getProcess_content_id().intValue() != notPassContentId && approval.getIf_approved() == 0) {
                    if(nextContentId == -1) {
                        nextContentId = approval.getProcess_content_id();
                        nextApprovals.add(approval);
                    } else if(approval.getProcess_content_id().intValue() == nextContentId) {
                        nextApprovals.add(approval);
                    }
                }
            }
            process.setProcess_rule_current_node(nextContentId);
            // 获取当前大节点的审批方式
            ProcessRuleContent processRuleContent = processRuleContentDao.getProcessRuleContentById(nextContentId);
            String task_mode = processRuleContent.getTask_mode();
            // 获取下一所有节点的审批人字符串
            String allApprover = "";
            for (Approval approval : nextApprovals) {
                allApprover = allApprover.equals("") ? approval.getNode_handler() : allApprover + "," + approval.getNode_handler();
            }
            process.setAll_handler(allApprover);
            if(!task_mode.equals("order")) {
                // 如果不是顺签
                process.setCurrent_task_handler(allApprover);
            } else {
                // 如果是顺签
                process.setCurrent_task_handler(allApprover.split(",")[0]);
            }
            // 更新流程
            processDao.updateInApprovalProcess(process);
        }
    }

    @Override
    public Map<String, Object> getSteps(Integer process_id, Integer tenant_id) {
        Map<String, Object> res = new HashMap<>();
        List<Object> list = new ArrayList<>();
        Process process = processDao.getProcessById(process_id);
        List<String> taskMode = new ArrayList<>();
        List<ProcessRuleContent> processRuleContentList = processRuleContentDao.getProcessRuleContent(process.getProcess_rule_code(), tenant_id);
        // 记录跳过了几个节点:有提交节点，所以从1计数
        List<Approval> temp_approvalList = approvalDao.getTargetApprovalByProcessId(process_id);
        Integer jumpNodeNum = 1;
        if(process.getProcess_rule_current_node() == null) {
            res.put("active", processRuleContentList.size() + 1);
        } else {
            for (int i = 0; i < processRuleContentList.size(); i++) {
                Integer content_id = processRuleContentList.get(i).getId();
                for (Approval approval : temp_approvalList) {
                    if(approval.getProcess_content_id().intValue() == content_id) {
                        jumpNodeNum += 1;
                        break;
                    }
                }
                jumpNodeNum -= 1;
                if(content_id == process.getProcess_rule_current_node()) {
                    res.put("active", i + 1 + jumpNodeNum);
                    break;
                }
            }
        }
        for (ProcessRuleContent processRuleContent : processRuleContentList) {
            if(processRuleContent.getTask_mode() != null && !processRuleContent.getTask_mode().equals("")) {
                taskMode.add(processRuleContent.getTask_mode());
            }
        }
        res.put("task_mode", taskMode);
        Employee submit = employeeDao.getEmployeeByEmployeeId(process.getSubmit_employee_id(), tenant_id);
        list.add(organizationalStructureService.getAllKindsDept(submit));
        List<Approval> approvalList = approvalDao.getTargetApprovalByProcessId(process_id);
        List<Integer> nodes = new ArrayList<>();
        for (Approval approval : approvalList) {
            if(nodes.size() == 0 || !nodes.contains(approval.getProcess_content_id())) {
                nodes.add(approval.getProcess_content_id());
            }
            if(
                approval.getOperate() != null && !approval.getOperate().equals("") &&
                (approval.getOperate().equals("Reject") || approval.getOperate().equals("stop"))
            ) {
                res.put("reject", true);
            }
        }
        for (Integer node : nodes) {
            List<Approval> temp = new ArrayList<>();
            for (Approval approval : approvalList) {
                if(node == approval.getProcess_content_id()) {
                    temp.add(approval);
                }
            }
            if(temp.size() == 1) {
                Employee employee = employeeDao.getEmployeeByEmployeeId(temp.get(0).getNode_handler(), tenant_id);
                list.add(organizationalStructureService.getAllKindsDept(employee));
            } else {
                List<Employee> employeeList = new ArrayList<>();
                for (Approval approval : temp) {
                    Employee employee = employeeDao.getEmployeeByEmployeeId(approval.getNode_handler(), tenant_id);
                    employeeList.add(organizationalStructureService.getAllKindsDept(employee));
                }
                list.add(employeeList);
            }
        }
        res.put("nodes", list);
        if(!res.containsKey("reject")) {
            res.put("reject", false);
        }
        return res;
    }
}
